package com.example.musicplayer.user;

import android.graphics.Bitmap;

import com.example.musicplayer.commonUtils.DrawUtil;
import com.example.musicplayer.musicClass.MusicInfo;

import java.util.ArrayList;
import java.util.List;

public class User {
    private static User sInstance;
    private final int mUserId;
    private final int mUserAuthority;
    private String mUserName;
    private int mUserPhoto;
    private int mUserVip;
    private int mUserGrade;
    private String mUserEmail;

    private User(int id, String name, int photo, int vip, int grade) {
        mUserId = id;
        mUserName = name;
        mUserPhoto = photo;
        mUserVip = vip;
        mUserGrade = grade;
        if (mUserId == 1) {
            mUserAuthority = 1;
        } else {
            mUserAuthority = 0;
        }
        mUserEmail = null;
    }

    public static void initUser(int id, String name, int photo, int vip, int grade, int authority) {
        if (sInstance == null || sInstance.getUserId() != id) {
            sInstance = new User(id, name, photo, vip, grade);
        }
    }

    public static User getInstance() {
        UserManage.setUserLogin(sInstance != null);
        return sInstance;
    }

    public static void exitLoginState() {
        UserManage.setUserLogin(false);
        sInstance = null;
    }

    public static Bitmap getUserPhoto(int photo) {
        MusicInfo musicInfo = MusicInfo.getMusicInfoById(photo);
        if (musicInfo == null) {
            return DrawUtil.getDefaultMusicMenuBitmap();
        }
        return musicInfo.getBitmap();
    }

    public int getUserId() {
        return mUserId;
    }

    public String getUserName() {
        return mUserName;
    }

    public String getUserEmail() {
        if (mUserEmail == null) {
            mUserEmail = UserManage.getUserEmail();
        }
        return mUserEmail;
    }

    public Bitmap getUserPhoto() {
        MusicInfo musicInfo = MusicInfo.getMusicInfoById(mUserPhoto);
        if (musicInfo == null) {
            return DrawUtil.getDefaultMusicMenuBitmap();
        }
        return musicInfo.getBitmap();
    }

    public int getUserPhotoId() {
        return mUserPhoto;
    }

    public String getPasswordProtect() {
        return UserManage.getPasswordProtect();
    }

    public String getPasswordProtectAnswer() {
        if (!UserManage.isPasswordConfirmSuccess()) {
            return "";
        }
        return UserManage.getPasswordProtectAnswer();
    }

    public boolean setUserName(String newName) {
        if (mUserName.equals(newName)) {
            return true;
        }
        if (UserManage.updateUserName(mUserId, newName)) {
            mUserName = newName;
            return true;
        }
        return false;
    }

    public boolean setUserPhoto(int photoId) {
        if (photoId < 0) {
            return false;
        }
        if (UserManage.updateUserPhoto(photoId)) {
            mUserPhoto = photoId;
            return true;
        }
        return false;
    }

    public boolean setUserPassword(String newPassword) {
        if (newPassword == null || newPassword.equals("")) {
            return false;
        }
        if (!UserManage.isPasswordConfirmSuccess()) {
            return false;
        }
        return UserManage.updatePassword(newPassword);
    }

    public boolean setPasswordProtect(String question, String answer) {
        if (UserManage.isPasswordConfirmSuccess() && question != null && answer != null) {
            return UserManage.updatePasswordProtect(question, answer);
        }
        return false;
    }

    public List<UserInfo> getAllUserInfo() {
        if (!hasAuthority()) {
            return new ArrayList<UserInfo>();
        }
        return UserManage.getAllUserInfo();
    }

    public boolean hasAuthority() {
        return mUserAuthority == 1;
    }
}